//
//  DiscoverViewController.m
//  MarianasTrench
//
//  Created by wangjue on 16/3/17.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "DiscoverViewController.h"
#import "RecommendVC.h"
#import "ExampleVC.h"
#import "AnchorVC.h"

@interface DiscoverViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *backScrollV;/**< 底层滑动的scrollview */
@property (nonatomic, strong) UIButton *recommendBtn;/**< 推荐按钮 */
@property (nonatomic, strong) UIButton *exampleBtn;/**< 榜单按钮 */
@property (nonatomic, strong) UIButton *anchorBtn;/**< 主播按钮 */
@property (nonatomic, strong) RecommendVC *recommendVC;/**< 推荐页面 */
@property (nonatomic, strong) ExampleVC *exampleVC;/**< 榜单页面 */
@property (nonatomic, strong) AnchorVC *anchorVC;/**< 主播页面 */
@property (nonatomic, strong) UIView *backView;/**< button下边的view */

@end

@implementation DiscoverViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"发现";
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    [self createView];
    
    
}
- (void)createView{
    
    // 推荐按钮
    self.recommendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _recommendBtn.frame = CGRectMake(0, 65, SCREEN_WIDTH / 3, 40);
    [_recommendBtn setTitle:@"推荐" forState:UIControlStateNormal];
    [self.view addSubview:_recommendBtn];
    [_recommendBtn addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _recommendBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_recommendBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    _recommendBtn.tag = 1000;
    
    // 榜单按钮
    self.exampleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _exampleBtn.frame = CGRectMake(SCREEN_WIDTH / 3, 64, SCREEN_WIDTH / 3, 40);
    [_exampleBtn setTitle:@"榜单" forState:UIControlStateNormal];
    [self.view addSubview:_exampleBtn];
    [_exampleBtn addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _exampleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_exampleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _exampleBtn.tag = 1001;
    
    // 主播按钮
    self.anchorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _anchorBtn.frame = CGRectMake(SCREEN_WIDTH / 3 * 2, 64, SCREEN_WIDTH / 3, 40);
    [_anchorBtn setTitle:@"主播" forState:UIControlStateNormal];
    [self.view addSubview:_anchorBtn];
    [_anchorBtn addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _anchorBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_anchorBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _anchorBtn.tag = 1002;

    // 创建底层的scrollview
    self.backScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 104, SCREEN_WIDTH, SCREEN_HETGHT - 153)];
    self.backScrollV.pagingEnabled = YES;
    self.backScrollV.contentSize = CGSizeMake(SCREEN_WIDTH * 3, 0);
    self.backScrollV.bounces = NO;
    self.backScrollV.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_backScrollV];
    self.backScrollV.delegate = self;
    self.backScrollV.backgroundColor = [UIColor redColor];
    
    // 创建三个viewcontoller
    self.recommendVC = [[RecommendVC alloc] init];
    // 设置容器视图控制器
    [self addChildViewController:_recommendVC];
    self.recommendVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT);
    self.recommendVC.view.backgroundColor = [UIColor yellowColor];
    [self.backScrollV addSubview:_recommendVC.view];
    
    self.exampleVC = [[ExampleVC alloc] init];
    [self addChildViewController:_exampleVC];
    self.exampleVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HETGHT);
    self.exampleVC.view.backgroundColor = [UIColor greenColor];
    [self.backScrollV addSubview:_exampleVC.view];
    
    self.anchorVC = [[AnchorVC alloc] init];
    [self addChildViewController:_anchorVC];
    self.anchorVC.view.frame = CGRectMake(SCREEN_WIDTH * 2, 0, SCREEN_WIDTH, SCREEN_HETGHT);
    [self.backScrollV addSubview:_anchorVC.view];
    
    // 设置button下边的view的动画
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(self.recommendBtn.frame.origin.x, 104, self.recommendBtn.frame.size.width, 2)];
    _backView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_backView];
    
    
    
    
}
#pragma mark - 三个button的点击方法
- (void)BtnAction:(UIButton *)button{
    // 下边的view滑动,button变色,切换页面
    
    if (button == self.recommendBtn) {
        [UIView animateWithDuration:0.2 animations:^{
            self.backView.frame = CGRectMake(button.frame.origin.x, 104, button.frame.size.width, 2);
        }];
        
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        for (NSInteger i = 1000; i <= 1002; i++) {
            if (button.tag != i) {
                UIButton *button = (UIButton *)[self.view viewWithTag:i];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
        self.backScrollV.contentOffset = CGPointMake(0, 0);
    } else if(button == self.exampleBtn){
        [UIView animateWithDuration:0.2 animations:^{
            self.backView.frame = CGRectMake(button.frame.origin.x, 104, button.frame.size.width, 2);
        }];
        
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        for (NSInteger i = 1000; i <= 1002; i++) {
            if (button.tag != i) {
                UIButton *button = (UIButton *)[self.view viewWithTag:i];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }

        
        self.backScrollV.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            self.backView.frame = CGRectMake(button.frame.origin.x, 104, button.frame.size.width, 2);
        }];
        
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        for (NSInteger i = 1000; i <= 1002; i++) {
            if (button.tag != i) {
                UIButton *button = (UIButton *)[self.view viewWithTag:i];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }


        self.backScrollV.contentOffset = CGPointMake(SCREEN_WIDTH * 2, 0);
    }
}
#pragma mark - scrollview的协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    [UIView animateWithDuration:0.2 animations:^{
        self.backView.frame = CGRectMake(scrollView.contentOffset.x / 3, 104, self.recommendBtn.frame.size.width, 2);
    }];
    
    UIButton *button = (UIButton *)[self.view viewWithTag:(scrollView.contentOffset.x / SCREEN_WIDTH + 1000)];

    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    for (NSInteger i = 1000; i <= 1002; i++) {
        if (button.tag != i) {
            UIButton *btn = (UIButton *)[self.view viewWithTag:i];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
